﻿using UnityEngine;
using System.IO;

public class ProtoData<T> where T : class,ProtoBuf.IExtensible 
{
	T[] m_dataItems;

	public ProtoData( byte[] buf )
	{
		MemoryStream ms = new MemoryStream( buf );
		BinaryReader br = new BinaryReader( ms );

		string typename = br.ReadString()+",m-client-proto";
		uint size = br.ReadUInt32();
//		Debug.Log( typename );
		m_dataItems = new T[size];

		System.Type type = System.Type.GetType( typename );
		if( !typeof(T).IsAssignableFrom(type) )
		{
			Debug.LogError(typename + " does not matched");
			return;
		}

		for( int i=0; i<size; i++ )
		{
			int len = br.ReadInt32();
			byte[] itemBuf = br.ReadBytes( len );

			m_dataItems[i] = ProtoBuf.Serializer.NonGeneric.Deserialize( type,new MemoryStream( itemBuf) ) as T;
		}
	}

	public int Count
	{
		get
		{
			return m_dataItems.Length;
		}
	}

	public T this[int index]
	{
		get
		{
			return m_dataItems[index];
		}
	}
}
